﻿namespace HynuIOTA.Web.Helpers
{
    using System;
    using HynuIOTA.Entity;
    using HynuIOTA.Web.Models;
    using HynuIOTA.Helpers;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using System.Reflection;

    public static class Tools
    {
        #region 获得用户IP
        /// <summary>
        /// 获得用户IP
        /// </summary>
        public static string GetUserIp()
        {
            return System.Web.HttpContext.Current.Request.UserHostAddress;
        }
        #endregion

        #region 设置属性值
        public static void SetValues<T, K>(T target, K orgin, string[] modifiedPropertyNames)
        {
            var targetType = target.GetType();
            var orginType = orgin.GetType();

            foreach (var propertyName in modifiedPropertyNames)
            {
                PropertyInfo tagetInfo = targetType.GetProperty(propertyName);
                PropertyInfo orginInfo = orginType.GetProperty(propertyName);

                if (tagetInfo == null || orginInfo == null)
                {
                    continue;
                }

                if (tagetInfo.PropertyType == orginInfo.PropertyType)
                {
                    tagetInfo.SetValue(target, orginInfo.GetValue(orgin));
                }
            }
        }
        #endregion
    }
}
